package com.adee.jdk8;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class Test006_ParallelStream {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>(5000000);
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (int i = 0; i < 5000000; i++) {
            list.add(random.nextInt());
        }
        System.out.println("开始排序");
        long start = System.currentTimeMillis();
        // 耗时大概 4000ms
        // list.stream().sorted().mapToLong(i -> i).max().ifPresent(System.out::println);
        // 耗时大概 2000ms，数据量较大时，并发流效率明显提高
        list.parallelStream().sorted().mapToLong(i -> i).max().ifPresent(System.out::println);
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));

    }
}
